package com.example.listossimage.threadpool;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.springframework.stereotype.Service;

import java.util.concurrent.*;

@Service
public class ThreadPoolTestService {


    public void testThread() {

        int max = 100000;
        int nThreads = Runtime.getRuntime().availableProcessors();

        ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat("demo-pool-%d").build();

        ExecutorService pool = new ThreadPoolExecutor(5, 200, 0L,
                TimeUnit.MICROSECONDS, new LinkedBlockingDeque<Runnable>(1024), namedThreadFactory,
                new ThreadPoolExecutor.AbortPolicy());

        long l = System.currentTimeMillis();
        for (int i = 0; i < max; i++) {
            pool.execute(() -> {
                    System.out.println("hhhhhhh有线程池");
            });
        }
        long l2 = System.currentTimeMillis();
        System.out.println(l2-l);

        long l3 = System.currentTimeMillis();
        for (int i = 0; i < max; i++) {
            System.out.println("hhhhhhh");
        }
        long l4 = System.currentTimeMillis();
        System.out.println(l4-l3);
    }
}
